package com.easondongh.blog.service.impl;

import com.easondongh.blog.dao.BlogCollectionDao;
import com.easondongh.blog.dao.CommentDao;
import com.easondongh.blog.enums.ResultEnum;
import com.easondongh.blog.exception.BlogException;
import com.easondongh.blog.mapper.UserMapper;
import com.easondongh.blog.pojo.BlogCollection;
import com.easondongh.blog.pojo.Comment;
import com.easondongh.blog.pojo.User;
import com.easondongh.blog.service.UserService;
import com.easondongh.blog.utils.*;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author EasonDongH
 * @date 2020/3/19 16:42
 */
@Service
public class UserServiceImpl implements UserService {

    /**
     * 默认密码
     */
    private static final String DEF_PASSWORD = "123456";

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private BlogCollectionDao blogCollectionDao;
    @Autowired
    private CommentDao commentDao;

    @Override
    public User getByUsername(String username) {
        return userMapper.getByUsername(username);
    }

    @Override
    public void save(User user) {
        user.setCreatedTime(DateTimeUtils.DateToString(new Date()));
        userMapper.save(user);
    }

    @Override
    public void update(User user) {
        user.setUpdateTime(DateTimeUtils.DateToString(new Date()));
        userMapper.update(user);
    }

    @Override
    public void updateInfo(User user) {
        user.setUpdateTime(DateTimeUtils.DateToString(new Date()));
        userMapper.updateInfo(user);
    }

    @Override
    public User getById(Integer id) {
        return userMapper.getById(id);
    }

    @Override
    public void deleteById(Integer id) {
        userMapper.deleteById(id);
    }

    @Override
    public Page<User> getByPage(Page<User> page) {
        // 查询数据
        List<User> userList = userMapper.getByPage(page);
        page.setList(userList);
        // 查询总数
        int totalCount = userMapper.getCountByPage(page);
        page.setTotalCount(totalCount);
        return page;
    }

    @Override
    public void resetPwd(List<Integer> userIds) {
        if(CollectionUtils.isNotEmpty(userIds)) {
            List<User> users = this.userMapper.getByIds(userIds);
            users.forEach(u -> {
                u.setPassword(Md5Utils.toMD5(DEF_PASSWORD));
                this.userMapper.update(u);
            });
        }
    }

    @Override
    public void register(User user) {
        if(!StringUtils.isNullOrEmpty(this.userMapper.getByUsername(user.getUsername()))) {
            throw new BlogException(ResultEnum.USERNAME_IS_EXIST);
        }
        user.setCreatedTime(DateTimeUtils.DateToString(new Date()));
        this.userMapper.save(user);
    }

    @Override
    public Map<String, Integer> getCollectionAndCommentCount() {
        User loginUser = (User) ShiroUtils.getLoginUser();
        if(loginUser != null) {
            int collectionCount = this.blogCollectionDao.countByUserId(loginUser.getUserId());
            int commentCount = this.commentDao.countByCommentUser(loginUser.getUserId());
            Map<String,Integer> map = new HashMap<>(2);
            map.put("collectionCount", collectionCount);
            map.put("commentCount", commentCount);
            return map;
        }
        return null;
    }

    @Override
    public Page<BlogCollection> getCollectionByPage(Page<BlogCollection> page) {
        User loginUser = (User) ShiroUtils.getLoginUser();
        if(loginUser != null) {
            Pageable pageable = PageRequest.of(page.getCurrentPage() - 1, page.getPageSize());
            org.springframework.data.domain.Page p = this.blogCollectionDao.findByUserIdOrderByCollectionTimeDesc(loginUser.getUserId(), pageable);
            page.setTotalCount((int) p.getTotalElements());
            page.setTotalPage(p.getTotalPages());
            page.setList(p.getContent());
//            BlogCollection blogCollection = new BlogCollection();
//            blogCollection.setUserId(loginUser.getUserId());
//            Example example = Example.of(blogCollection);
//            org.springframework.data.domain.Page p = this.blogCollectionDao.findAll(example, pageable);
//            List<BlogCollection> list = new ArrayList<>(page.getList());
//            Collections.sort(list, (c1,c2)-> {
//                return c2.getCollectionTime().compareTo(c1.getCollectionTime());
//            });
//            page.setList(list);
        }
        return page;
    }

    @Override
    public Page<Comment> getCommentByPage(Page<Comment> page) {
        User loginUser = (User) ShiroUtils.getLoginUser();
        if(loginUser != null) {
            Pageable pageable = PageRequest.of(page.getCurrentPage() - 1, page.getPageSize());
            org.springframework.data.domain.Page p = this.commentDao.findByCommentUserOrderByCreatedTimeDesc(loginUser.getUserId(), pageable);
            page.setTotalCount((int) p.getTotalElements());
            page.setTotalPage(p.getTotalPages());
            page.setList(p.getContent());
        }
        return page;
    }

}
